package com.boot.controller;


import com.boot.AutoFillAnnotation.AutoFill;
import com.boot.AutoFillAnnotation.OperationType;
import com.boot.DTO.EmployeeAddDAO;
import com.boot.DTO.EmployeeLoginDTO;
import com.boot.Vo.ChangePasswordVo;
import com.boot.Vo.EmployeePageVo;
import com.boot.pojo.Employee;
import com.boot.result.Result;
import com.boot.result.ResultCodeNum;
import com.boot.service.EmployeeService;
import io.micrometer.common.util.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("employee")
@Tag(name = "后台用户相关接口",description = "后台管理应用用到的接口")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @Operation(summary = "用户登录")
    @PostMapping("login")
    public Result login(@RequestBody EmployeeLoginDTO employeeLoginDTO){
        if (StringUtils.isEmpty(employeeLoginDTO.getUsername()) || StringUtils.isEmpty(employeeLoginDTO.getPassword())){
            return Result.build(null, ResultCodeNum.USERNAME_OR_PASSWORD_MISSING);
        }
        return employeeService.login(employeeLoginDTO);
    }

    @Operation(summary = "用户退出")
    @PostMapping("logout")
    public Result<String> logout(){
        return Result.success();
    }

    @Operation(summary = "新增员工")
    @PostMapping()
    @AutoFill(value = OperationType.INSERT)
    public Result save(@RequestBody Employee employee, @RequestHeader String token){
        return employeeService.save(employee, token);
    }

    @Operation(summary = "获取员工列表")
    @GetMapping("page")
    public Result getEmployeePage(EmployeePageVo employeePageVo){
        return employeeService.getEmployeeList(employeePageVo);

    }


    @Operation(summary = "查询指定员工信息")
    @GetMapping("/{id}")
    public Result getEmployeeById(@PathVariable Long id){
        return employeeService.getEmployeeById(id);
    }

    @Operation(summary = "启用禁用员工")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,@RequestParam("id") Long userId){
        return employeeService.updateStatus(status, userId);
    }

    @Operation(summary = "编辑员工")
    @PutMapping()
    public Result putEmployee(@RequestBody EmployeeAddDAO employeeAddDAO, @RequestHeader String token) {
        return employeeService.updateEmployee(employeeAddDAO, token);
    }


    @Operation(summary = "修改密码")
    @PutMapping("editPassword")
    public Result editPassword(@RequestBody ChangePasswordVo changePasswordVo) {
        return employeeService.editPassword(changePasswordVo);
    }
}
